/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controladores;

import controladores.exceptions.IllegalOrphanException;
import controladores.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import modelo.Detallenomina;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import modelo.Nomina;

/**
 *
 * @author Ususario
 */
public class NominaJpaController implements Serializable {

    public NominaJpaController() {
    }

    public EntityManager getEntityManager() {
        return jpaConnection.getEntityManager();
    }

    public Nomina findByUltimo() {
        Query q = getEntityManager().createNamedQuery("Nomina.findByUltimo");
        q.setMaxResults(1);
        return (Nomina) q.getSingleResult();
    }

    public void create(Nomina nomina) {
        if (nomina.getDetallenominaList() == null) {
            nomina.setDetallenominaList(new ArrayList<Detallenomina>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Detallenomina> attachedDetallenominaList = new ArrayList<Detallenomina>();
            for (Detallenomina detallenominaListDetallenominaToAttach : nomina.getDetallenominaList()) {
                detallenominaListDetallenominaToAttach = em.getReference(detallenominaListDetallenominaToAttach.getClass(), detallenominaListDetallenominaToAttach.getIddetallenomina());
                attachedDetallenominaList.add(detallenominaListDetallenominaToAttach);
            }
            nomina.setDetallenominaList(attachedDetallenominaList);
            em.persist(nomina);
            for (Detallenomina detallenominaListDetallenomina : nomina.getDetallenominaList()) {
                Nomina oldNominaIdnominaOfDetallenominaListDetallenomina = detallenominaListDetallenomina.getNominaIdnomina();
                detallenominaListDetallenomina.setNominaIdnomina(nomina);
                detallenominaListDetallenomina = em.merge(detallenominaListDetallenomina);
                if (oldNominaIdnominaOfDetallenominaListDetallenomina != null) {
                    oldNominaIdnominaOfDetallenominaListDetallenomina.getDetallenominaList().remove(detallenominaListDetallenomina);
                    oldNominaIdnominaOfDetallenominaListDetallenomina = em.merge(oldNominaIdnominaOfDetallenominaListDetallenomina);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Nomina nomina) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Nomina persistentNomina = em.find(Nomina.class, nomina.getIdnomina());
            List<Detallenomina> detallenominaListOld = persistentNomina.getDetallenominaList();
            List<Detallenomina> detallenominaListNew = nomina.getDetallenominaList();
            List<String> illegalOrphanMessages = null;
            for (Detallenomina detallenominaListOldDetallenomina : detallenominaListOld) {
                if (!detallenominaListNew.contains(detallenominaListOldDetallenomina)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Detallenomina " + detallenominaListOldDetallenomina + " since its nominaIdnomina field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<Detallenomina> attachedDetallenominaListNew = new ArrayList<Detallenomina>();
            for (Detallenomina detallenominaListNewDetallenominaToAttach : detallenominaListNew) {
                detallenominaListNewDetallenominaToAttach = em.getReference(detallenominaListNewDetallenominaToAttach.getClass(), detallenominaListNewDetallenominaToAttach.getIddetallenomina());
                attachedDetallenominaListNew.add(detallenominaListNewDetallenominaToAttach);
            }
            detallenominaListNew = attachedDetallenominaListNew;
            nomina.setDetallenominaList(detallenominaListNew);
            nomina = em.merge(nomina);
            for (Detallenomina detallenominaListNewDetallenomina : detallenominaListNew) {
                if (!detallenominaListOld.contains(detallenominaListNewDetallenomina)) {
                    Nomina oldNominaIdnominaOfDetallenominaListNewDetallenomina = detallenominaListNewDetallenomina.getNominaIdnomina();
                    detallenominaListNewDetallenomina.setNominaIdnomina(nomina);
                    detallenominaListNewDetallenomina = em.merge(detallenominaListNewDetallenomina);
                    if (oldNominaIdnominaOfDetallenominaListNewDetallenomina != null && !oldNominaIdnominaOfDetallenominaListNewDetallenomina.equals(nomina)) {
                        oldNominaIdnominaOfDetallenominaListNewDetallenomina.getDetallenominaList().remove(detallenominaListNewDetallenomina);
                        oldNominaIdnominaOfDetallenominaListNewDetallenomina = em.merge(oldNominaIdnominaOfDetallenominaListNewDetallenomina);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = nomina.getIdnomina();
                if (findNomina(id) == null) {
                    throw new NonexistentEntityException("The nomina with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Nomina nomina;
            try {
                nomina = em.getReference(Nomina.class, id);
                nomina.getIdnomina();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The nomina with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<Detallenomina> detallenominaListOrphanCheck = nomina.getDetallenominaList();
            for (Detallenomina detallenominaListOrphanCheckDetallenomina : detallenominaListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Nomina (" + nomina + ") cannot be destroyed since the Detallenomina " + detallenominaListOrphanCheckDetallenomina + " in its detallenominaList field has a non-nullable nominaIdnomina field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(nomina);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Nomina> findNominaEntities() {
        return findNominaEntities(true, -1, -1);
    }

    public List<Nomina> findNominaEntities(int maxResults, int firstResult) {
        return findNominaEntities(false, maxResults, firstResult);
    }

    private List<Nomina> findNominaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Nomina.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Nomina findNomina(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Nomina.class, id);
        } finally {
            em.close();
        }
    }

    public int getNominaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Nomina> rt = cq.from(Nomina.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
}
